import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
    {
        path:"/login",
        component:()=>import("../views/login/index.vue")
    },
    {
        path:"/",
        redirect:"/user",
        component:()=>import("../views/home/index.vue"),
        children:[
            {
                path:"user",
                component:()=>import("../views/user/index.vue"),
            },
            {
                path:"crab",
                component:()=>import("../views/crab/index.vue"),
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
});

//路由守卫
router.beforeEach((to,from,next)=>{
    const isLogin=localStorage.token?true:false;
    if(to && to.path === "/" && isLogin){
        next("/");
    };
    if(to.path === "/login"){
        next();
    }else{
        if(isLogin){
            next();
        }else{
            next("/login");
        };
    };
});



export default router